﻿using DeviceMonitor.Model;
using DeviceMonitor.Utils;
using DeviceMonitor.ViewModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Org.BouncyCastle.Utilities;

namespace DeviceMonitor.Pages
{
    /// <summary>
    /// AlermPage2.xaml 的交互逻辑
    /// </summary>
    public partial class AlermPage2new : Page
    {
        AlermViewModel viewModel;
        SynchronizationContext _syncContext = null;
        public AlermPage2new()
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            viewModel = new AlermViewModel();

            viewModel.queryParam.timeBeggin = DateTime.Now.ToString("yyyy-MM-dd");
            viewModel.queryParam.timeEnd = DateTime.Now.ToString("yyyy-MM-dd");
            //  loadData();


            this.Loaded += Page_loaded;
            this.DataContext = viewModel;

        }



        private void Page_loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        private void loadData()
        {

            new Task(() =>
            {
                try
                {
                    JObject paramJSON = new JObject();
                    
                    String response = HttpPostHelper.doHttpPost("/alerm/queryAlermList", paramJSON.ToString());
                    JObject rspJson = JObject.Parse(response);
                    bool success = (bool)rspJson["success"];
                    if (success)
                    {
                        var jsonStr = rspJson["data"]["dataList"].ToString();
                        JArray dataListArray = (JArray)rspJson["data"]["dataList"];
                        String endTime= (string)rspJson["data"]["lastQueryTime"];
                        viewModel.queryParam.timeBeggin = endTime;
                        // 将 JArray 反序列化为 List<MyDataClass>
                        List<AlermModel> dataList = JsonConvert.DeserializeObject<List<AlermModel>>(dataListArray.ToString());
                        //foreach (AlermModel item in dataList)
                        //{ item.currentQueryTime = endTime;

                        //}



                            // 现在你可以使用 dataList 了
                            // List<AlermModel> items = JsonConvert.DeserializeObject<List<AlermModel>>(jsonStr);

                            _syncContext.Post(SetDataList, dataList);
                      
                
                    }
                }
                catch (Exception ex) { }
            }).Start();
        }
        private void SetDataList(Object obj)
        {
            List<AlermModel> items = (List<AlermModel>)obj;
            viewModel.list.Clear();
            foreach (var item in items)
            {
                viewModel.list.Add(item); // 添加新数据
            }
        }

        private void Button_Click_Query(object sender, RoutedEventArgs e)
        {
            this.loadData();
        }
    }
}
